//生成验证码

function createCode(){
    $(function () {
        var code="";//变量
        var codeLength=4;
        var selectChar=new Array(2,3,4,5,7,8,9,'a','c','d','e','f','g','h','i','j','k','m','n','p','q','r','s','t','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z');
        for(var i=0;i<codeLength;i++){
            var charIndex=Math.floor(Math.random()*52);
            code += selectChar[charIndex];
        }
        if(code.length!=codeLength){
            createCode();
        }
        $("#checkCode").val(code)
    })
}
//调用验证码
createCode();
// $(function(){
//     $("#btn-login").click(function () {
//         if(toUpperCase($("#code").val())==toUpperCase($("#checkCode").val())){
//
//         }else if(!!$("#code").val()){
//             alert("验证码为false");
//             createCode();
//         }else{
//             alert("验证码不能Null");
//             createCode();
//         }
//     });
// });
// $(function(){
//     function check(){
//         var username=$("#username").val().replaceAll(/\s+/g,"")
//         var password=$("#password").val().replaceAll(/\s+/g,"")
//         var code=$("#code").val().replaceAll(/\s+/g,"")
//         if(username==null||username==""){
//             alert("用户名不能为空")
//         }else{
//             alert("手机号长度小于11位")
//         }
//     }
// })
function isLogin() {
    var username = $("#username").val()
    if (!username) {
        alert("用户名不能为空。");
        createCode();
        return false;
    }
    var password = $("#password").val();
    if (!password) {
        alert("密码不能为空。");
        createCode();
        return false;
    }
    if (password.length < 6 ) {
        alert("密码至少6位。");
        createCode();
        return false;
    }
    var code = $("#code").val();
    if (!code) {
        alert("验证码不能为空。");
        createCode()
        return false;
    }
    var codes=($("#code").val()).toLowerCase()
    var checkCodes=($("#checkCode").val()).toLowerCase()
    if (codes != checkCodes) {
        alert("验证码错误。");
        createCode();
        return false;
    }
    return true;

}
//登录
$(function () {
    $("#btn").click(function () {

        if(!isLogin()){
            return;
        }
        parArr = {};
        data = {};
        var username = $("#username").val()
        parArr.userName = username
        var params = JSON.stringify(parArr);
        $.ajax({
            type: "POST",
            url: "/informationentry/publicKey",
            data: params,
            async: true,
            contentType: "application/json",
            dataType: "json",
            success: function (result) {
                if(result.statusCode == "2000"){
                    publicKey = result.data.publicKey;
                    var encrypt = new JSEncrypt();
                    encrypt.setPublicKey(publicKey);
                    // 密码加密
                    var pwd = $("#password").val();
                    var d = encrypt.encrypt(pwd);
                    console.log(d);
                    parArr.passWord = d;
                    var data = JSON.stringify(parArr);
                    // 登录
                    $.ajax({
                        url: "/informationentry/login",
                        contentType: "application/json",
                        async: true,
                        datatype: "json",
                        data: data,
                        type: 'POST',
                        success: function (result) {
                            if (result.statusCode == "2000") {
                                // // 获取token存入cookie   $.cookie(key, value, {path: '存放地址'});
                                $.cookie('token', result.data.userToken, {path: '/'});
                                $.cookie('username', result.data.userPhone, {path: '/'});
                                $.cookie('name',result.data.userName, {path: '/'});
                                // 跳转到主页
                                window.location.href = '/index.html';
                                // return index.html
                            }else {
                                alert(result.message);
                            }


                        }
                    })
                }else {
                    alert(result.message);
                }

            }
        })
    })
})


// //密码加密
// function encryptionData(password){
//     var options={
//         "encryptionType":"RSA",
//         "encryptionData":password,
//         "publicKey":localStorage.publicKey
//     };
//     alert(options);
//     $.ajax({
//         type:"POST",
//         url:"/encryption",
//         data:JSON.stringify(options),
//         async:true,
//         contentType:"application/json",
//         dataType:"json",
//         success:function(result){
//             // var data=JSON.stringify(result.data);
//             // alert(data);
//             var data=result.data;
//             localStorage.setItem('newEncryptionData',data.newEncryptionData);
//         }
//     })
// }


//注册
// function regisData(username, userphone) {
//     var options = {
//         "userName": username,
//         "passWord": localStorage.newEncryptionData,
//         "userPhone": userphone
//     };
//     alert(options);
//     $.ajax({
//         type: "POST",
//         url: "/regis",
//         data: JSON.stringify(options),
//         async: true,
//         contentType: "application/json",
//         dataType: "json",
//         success: function (result) {
//             var data = JSON.stringify(result);
//             alert(data);
//         }
//     })


//添加用户
$(function() {
    $("#add-user-brt").click(function () {
        if(!userAdd()){
            return;
        }
        Confirm("确认添加？", function () {
            parArr = {};
            parArr.userName = $("#userphone").val();
            var params = JSON.stringify(parArr);
            $.ajax({
                type: "POST",
                url: "/informationentry/publicKey",
                data: params,
                async: true,
                contentType: "application/json",
                dataType: "json",
                success: function (result) {
                    cookieNull(result.statusCode)
                    if (result.statusCode == "2000") {
                        publicKey = result.data.publicKey;
                        var encrypt = new JSEncrypt();
                        encrypt.setPublicKey(publicKey);
                        // 密码加密
                        var pwd = $("#password").val();
                        var newPassWord = encrypt.encrypt(pwd);
                        var option = {
                            "userName": $.cookie("username"),
                            "token": $.cookie("token"),
                            "newUserName": $("#username").val(),
                            "newUserPhone": $('#userphone').val(),
                            "newPassWord": newPassWord
                        }
                        $.ajax({
                            type: "POST",
                            url: "/informationentry/regis",
                            data: JSON.stringify(option),
                            async: true,
                            contentType: "application/json",
                            dataType: "json",
                            success: function (result) {
                                cookieNull(result.statusCode)
                                if (result.statusCode == "2000") {
                                    Confirm(result.message)
                                    $("input[ type='text']").val("")
                                } else {
                                    Confirm(result.message)
                                    $("input[ type='text']").val("")
                                }
                            }

                        })

                        // var url = "/informationentry/regis";
                        // var data = JSON.stringify(option);
                        // ajax(url, data);

                        //
                        // }else{
                        //     Confirm(value.message)
                        // }

                    }
                }

            })
        })
    })
})
// 修改密码
$(function(){
    $("#apdatePwd").click(function () {
        if(!updatePwd()){
            return;
        }
        Confirm("温馨提示","确认添加？",function(){
            parArr = {};
            var username = $.cookie("username")
            parArr.userName = username;
            var params = JSON.stringify(parArr);
            $.ajax({
                type: "POST",
                url: "/informationentry/publicKey",
                data: params,
                async: true,
                contentType: "application/json",
                dataType: "json",
                success: function (result) {
                    if (result.statusCode == "2000") {
                        publicKey = result.data.publicKey;
                        var encrypt = new JSEncrypt();
                        encrypt.setPublicKey(publicKey);
                        // 密码加密
                        var oldpwd = $("#oldPassWord").val();
                        var newpwd = $("#newPassWord").val()
                        var newPassWord = encrypt.encrypt(newpwd);
                        var oldPassWord = encrypt.encrypt(oldpwd);
                        var option = {
                            "userName":username,
                            "token": $.cookie("token"),
                            "oldPassWord":oldPassWord,
                            "newPassWord":newPassWord
                        }
                        var url="/informationentry/updateUser";
                        var data=JSON.stringify(option)
                        ajax(url,data)
                        cookieNull(value.statusCode)
                        if(value.statusCode=="2000"){
                            Confirm(value.message)
                            $("input[ type='text']").val("")
                            Logout();
                        }else{
                            Confirm(value.message)
                            $("input[ type='text']").val("")
                        }
                    }
                }
            })
        })

    })
})

// 注销

function Logout(){
    Confirm("确认注销？",function(){
        $.cookie("token",null);
        $.cookie("username",null);
        $.cookie("name",null);
        window.location.href = '/login.html';

    })
}
